import { Component, OnInit, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-price-quote',
  templateUrl: './price-quote.component.html',
  styleUrls: ['./price-quote.component.css']
})
export class PriceQuoteComponent implements OnInit {

  stockCode: string = "IBM";
  price: number;

  @Output('priceChange')  // lastPrice名字和父组件自定义事件的名字要对应就是括号里面的，如果Output里面存在名字，则要和父组件自定义事件的名字对应
  lastPrice: EventEmitter<PriceQuote> = new EventEmitter();  // 泛型是要发射的事件是什么类型

  @Output()
  buy: EventEmitter<PriceQuote> = new EventEmitter();
  constructor() {
    setInterval(() => {
      let priceQuote: PriceQuote = new PriceQuote(this.stockCode, 100 * Math.random());
      this.price = priceQuote.lastPrice;
      this.lastPrice.emit(priceQuote);
    }, 3000 );
  }

  ngOnInit() {
  }

  buyStock(event) {
    this.buy.emit(new PriceQuote(this.stockCode, this.price) );
  }

}

export class PriceQuote {
  constructor(
    public stockCode: string ,
    public lastPrice: number
  ) {}
}
